package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.User;

public class UserDAO {

	public static User getAUser(Connection conn, String uName){
		User user = new User();
		String GET_A_USER = "select * from easy_user where user_name = ?";
		try{
			PreparedStatement pstmt = Conn.preparedStatement(conn, GET_A_USER);
			pstmt.setString(1, uName);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				user.setUID(rs.getInt(1));
				user.setUName(rs.getString(2));
				user.setUPassword(rs.getString(3));
				user.setUPhone(rs.getString(4));
				user.setUAddress(rs.getString(5));
				user.setUExpertFlag(rs.getString(6));
				user.setUEmail(rs.getString(7));
				user.setULoginFlag(rs.getString(8));
				user.setUType(rs.getString(9));
			}
			Conn.close(rs);
			Conn.close(pstmt);
		} catch (SQLException e){
			e.printStackTrace();
		} 
		
		return user;
	}
	
	public static boolean updateLoginFlag(Connection conn, String uName){
		boolean flag = false;
		String UPDATE_LOGIN_FLAG = "update easy_user set user_login_flag = 'Y' where user_name = ?";
		try{
			PreparedStatement pstmt = Conn.preparedStatement(conn, UPDATE_LOGIN_FLAG);
			pstmt.setString(1, uName);
			int rows = pstmt.executeUpdate();
			if(rows == 1)
				flag = true;
			Conn.close(pstmt);
		} catch (SQLException e){
			e.printStackTrace();
		} 
		
		return flag;
	}
	public static boolean logout(Connection conn, String uName){
		boolean flag = false;
		String UPDATE_LOGIN_FLAG = "update easy_user set user_login_flag = 'N' where user_name = ?";
		try{
			PreparedStatement pstmt = Conn.preparedStatement(conn, UPDATE_LOGIN_FLAG);
			pstmt.setString(1, uName);
			int rows = pstmt.executeUpdate();
			if(rows == 1)
				flag = true;
			Conn.close(pstmt);
		} catch (SQLException e){
			e.printStackTrace();
		} 
		
		return flag;
	}
}
